<?php

if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Dayrui Website Management System
 *
 * @since		version 2.0
 * @author		Dayrui <dayrui@gmail.com>
 * @license     http://www.dayrui.com/license
 * @copyright   Copyright (c) 2011 - 9999, Dayrui.Com, Inc.
 */

class Update extends M_Controller {

    /**
     * 构造函数
     */
    public function __construct() {
        parent::__construct();
        $this->db->db_debug = TRUE;
    }

    /**
     * 更新程序
     */
    public function index() {


        // 删除不需要的文件
        $this->load->helper('file');
        delete_files(FCPATH.'dayrui/libraries/Chart/', TRUE);
        delete_files(FCPATH.'dayrui/libraries/tmp-upload-images/', TRUE);
        $page = (int)$this->input->get('page');
        if (!$page) {
            $this->load->model('system_model');
            $tableinfo = $this->system_model->cache();
            $this->set_cache_data('table2', $tableinfo, 3600);
            $this->admin_msg('正在升级数据...', dr_url('update/index', array('page' => $page + 1)), 2);
        }
        // 表结构缓存
        $tableinfo = $this->get_cache_data('table2');
        if (!$tableinfo) {
            $this->load->model('system_model');
            $tableinfo = $this->system_model->cache();
            $this->set_cache_data('table2', $tableinfo, 3600);
        }
        switch($page) {
            case 1:

                $version = require FCPATH.'config/version.php';

                // 升级语言文件
                $module = @file_get_contents(FCPATH.'cache/update/module_lang.php');
                $category = @file_get_contents(FCPATH.'cache/update/category_lang.php');

                // 模块部分
                $local = @array_diff(dr_dir_map(FCPATH, 1), array('app', 'cache', 'config', 'dayrui', 'member', 'space', 'player')); // 搜索本地模块
                if ($local) {
                    foreach ($local as $dir) {
                        $file = FCPATH.$dir.'/language/zh-cn/category_lang.php';
                        if (is_file($file)) {
                            $this->set_lang($file, $version, $category);
                        }
                        $file = FCPATH.$dir.'/language/zh-cn/module_lang.php';
                        if (is_file($file)) {
                            $this->set_lang($file, $version, $module);
                        }
                    }
                }

                // 网站部分
                $admin = @file_get_contents(FCPATH.'cache/update/admin_lang.php');
                $this->set_lang(FCPATH.'dayrui/language/zh-cn/admin_lang.php', $version, $admin);

                $template = @file_get_contents(FCPATH.'cache/update/template_lang.php');
                $this->set_lang(FCPATH.'dayrui/language/zh-cn/admin_lang.php', $version, $template);

                $member = @file_get_contents(FCPATH.'cache/update/member_lang.php');
                $this->set_lang(FCPATH.'member/language/zh-cn/member_lang.php', $version, $member);

                $this->admin_msg('语言包升级完成...', dr_url('update/index', array('page' => $page + 1)), 2);
                break;


            default:
                $this->admin_msg('升级完成，请更新全站缓存在刷新页面', '', 1);exit;
                break;
        }
    }

    private function set_lang($file, $version, $code) {

        if (!$code) return;

        // 判断新的语言标示是否存在在旧文件中
        if (preg_match("/\[\'(.*)\'\]/", $code, $a)) {
            $data = file_get_contents($file);
            if (strpos($data, "'".$a[1]."'") === false) {
                $a = file_put_contents($file, $data.PHP_EOL.PHP_EOL.'/**更新程序增加部分 v'.$version['DR_VERSION'].'**/'.PHP_EOL.$code);
				if (!$a) {
					exit($file.'：不可写入，请给权限！！');
				}
			}
        }
    }
}